package com.mnyc.research.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.mnyc.common.annotation.Excel;
import com.mnyc.common.utils.DateUtils;
import com.mnyc.common.utils.DictUtils;
import com.mnyc.common.utils.SecurityUtils;
import com.mnyc.research.domain.JyResearchGroupTeacher;
import com.mnyc.research.mapper.JyResearchGroupTeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mnyc.research.mapper.JyResearchGroupMapper;
import com.mnyc.research.domain.JyResearchGroup;
import com.mnyc.research.service.IJyResearchGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 教研组Service业务层处理
 *
 * @author wdy
 * @date 2025-08-06
 */
@Service
public class JyResearchGroupServiceImpl implements IJyResearchGroupService {
    @Autowired
    private JyResearchGroupMapper jyResearchGroupMapper;
    @Autowired
    private JyResearchGroupTeacherMapper groupTeacherMapper;

    /**
     * 查询教研组
     *
     * @param id 教研组主键
     * @return 教研组
     */
    @Override
    public JyResearchGroup selectJyResearchGroupById(Long id) {
        JyResearchGroup jyResearchGroup = jyResearchGroupMapper.selectJyResearchGroupById(id);
        JyResearchGroupTeacher teacherQuery = new JyResearchGroupTeacher();
        teacherQuery.setGroupId(id);
        List<JyResearchGroupTeacher> teachers = groupTeacherMapper.selectJyResearchGroupTeacherList(teacherQuery);
        jyResearchGroup.setTeacherList(teachers);
        return jyResearchGroup;
    }

    /**
     * 查询教研组列表
     *
     * @param jyResearchGroup 教研组
     * @return 教研组
     */
    @Override
    public List<JyResearchGroup> selectJyResearchGroupList(JyResearchGroup jyResearchGroup) {
        return jyResearchGroupMapper.selectJyResearchGroupList(jyResearchGroup);
    }

    /**
     * 新增教研组
     *
     * @param group 教研组
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJyResearchGroup(JyResearchGroup group) {
        group.setCreateBy(SecurityUtils.getUsername());
        group.setCreateTime(DateUtils.getNowDate());
        group.setName(getName(group));
        int i = jyResearchGroupMapper.insertJyResearchGroup(group);
        saveGroupTeachers(group);
        return i;
    }

    /**
     * 修改教研组
     *
     * @param group 教研组
     * @return 结果
     */
    @Transactional
    @Override
    public int updateJyResearchGroup(JyResearchGroup group) {
        group.setUpdateBy(SecurityUtils.getUsername());
        group.setUpdateTime(DateUtils.getNowDate());
        group.setName(getName(group));
        saveGroupTeachers(group);
        return jyResearchGroupMapper.updateJyResearchGroup(group);
    }

//    /**
//     * 批量删除教研组
//     *
//     * @param ids 需要删除的教研组主键
//     * @return 结果
//     */
//    @Transactional
//    @Override
//    public int deleteJyResearchGroupByIds(Long[] ids) {
//        return jyResearchGroupMapper.deleteJyResearchGroupByIds(ids);
//    }

    /**
     * 删除教研组信息
     *
     * @param id 教研组主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJyResearchGroupById(Long id) {
        groupTeacherMapper.deleteByGroupId(id);
        return jyResearchGroupMapper.deleteJyResearchGroupById(id);
    }

    void saveGroupTeachers(JyResearchGroup group) {
        groupTeacherMapper.deleteByGroupId(group.getId());
        List<JyResearchGroupTeacher> teacherList = group.getTeacherList();
        Assert.isTrue(CollUtil.isNotEmpty(teacherList), "教师不能为空");
        for (JyResearchGroupTeacher teacher : teacherList) {
            teacher.setGroupId(group.getId());
            groupTeacherMapper.insertJyResearchGroupTeacher(teacher);
        }
    }

    String getName(JyResearchGroup group) {
        String semester = DictUtils.getDictLabel("jy_semester", group.getSemester());
        String subject = DictUtils.getDictLabel("jy_subject", group.getSubject());
        String grade = DictUtils.getDictLabel("jy_grade", group.getGrade());
        String join = String.join("-", semester, subject, grade);
        return join;
    }


}
